package com.jec.module.sysconfig.service.configor;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;

import com.jec.module.sysconfig.entity.NetUnitConfig;
import com.jec.module.sysconfig.service.convertor.Convertor;
import com.jec.module.sysmanage.service.SysLogService;
import com.jec.module.sysmonitor.dao.NetUnitRepository;
import com.jec.module.sysmonitor.entity.NetUnit;
import com.jec.utils.Response;

import lombok.Getter;
import lombok.Setter;

public abstract class BaseConfigor <T> {
	
	//public static Map<Object, BaseConfigor<?>> map = Maps.newHashMap();
	
    @Autowired
    protected NetUnitRepository netUnitRepository;
    
    @Autowired
    protected SysLogService sysLogService;
    
    protected String logKey = "";
	
	public BaseConfigor() {
	}
	
	protected abstract JpaRepository<T, Integer> getRepository();
	
	protected abstract Convertor<T> getConvertor();
	
	public abstract boolean hasConfigOnNetunit(int netunit);

	public Response saveOrUpdate(T config, boolean saveOrUpdate) {
		
		JpaRepository<T, Integer> repo = getRepository();
		
		if(config instanceof NetUnitConfig) {
			
			NetUnitConfig nc = (NetUnitConfig)config;
			
			Response response = nc.validate();
			
	        if(response!= null && (Integer)response.get("status") !=  Response.STATUS_SUCCESS)
	            return response;
	        
	        Convertor<T> convertor = this.getConvertor();
	        if(convertor!=null){
	            response = convertor.process(config);
	            if((Integer)response.get("status") !=  Response.STATUS_SUCCESS)
	                return response;
	        }
	        
			response = Response.Builder().status(Response.STATUS_PARAM_ERROR);
			
			NetUnit netunit = null;
			
	        if(nc.getNetunit() != -1) {
	        	netunit = netUnitRepository.findOne(nc.getNetunit());
	            if (netunit == null)
	                return response.message("网元错误");
	        }
	     
	        nc.setUpdateDate();
	        
	        config = repo.save(config);
	        
	        String desc= saveOrUpdate ? "新建" : "修改";
	        
	        if(netunit != null) {
	            desc += netunit.getName() + "的";
	        }
	        
	        desc += logKey + "配置";
	        
	        sysLogService.addLog(desc);

	        return response.status(Response.STATUS_SUCCESS).data(config);
			
		} else {
			
			return Response.Builder().status(Response.STATUS_PARAM_ERROR);
			
		}
		
	}

	public SearchResult<T> load(int page, int pageSize) {
		
		PageRequest pr = new PageRequest(page - 1, pageSize);
		
		JpaRepository<T, Integer> repo = getRepository();
		
		Page<T> found = repo.findAll(pr);
		
		SearchResult<T> result = new SearchResult<T>();
		
		result.setResult(found.getContent());
		result.setTotalCount((int)found.getTotalElements());
		return result;
		
	}
	
	public boolean delete(int id, int netunit) {
		
		JpaRepository<T, Integer> repo = getRepository();
		
		if (id > 0) {
			
			T config = repo.findOne(id);
			if(config == null) return false;
			
			String netunitName = "";
			if(config instanceof NetUnitConfig) {
				
				NetUnitConfig nc = (NetUnitConfig)config;
				int netunitId = nc.getNetunit();
	            if(netunitId > 0) {
	            	netunitName = netUnitRepository.findOne(netunitId).getName();
	            }
								
			}

            repo.delete(id);
            
            sysLogService.addLog("删除"+ netunitName + logKey + "配置");
            
            return true;
            
        } else {
        	
        	String netunitName = netUnitRepository.findOne(netunit).getName();
        	
            repo.delete(netunit);
            
            sysLogService.addLog("删除"+ netunitName + logKey + "配置");
            
            return true;
        }
		
	}
	
	public static class SearchResult<T> {
		
		@Getter @Setter
		List<T> result = null;
		
		@Getter @Setter
		long totalCount = 0;
		
	}
	
}
